/**
 * Copyright (C) 2007 Mustafa K. Isik - isik@acm.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

package com.paxcodi.rmi.calculator;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;

public class CalculatorClient {

    /**
     * Calculator for two floating point numbers.
     * 
     * Can add, subtract, divide and multiply. Outputs result on console.
     * 
     * @param args hostname of server and parameters in format <code>rmi://host.domain:port/calculator</code> parameter1 parameter2 operator
     */
    public static void main(String[] args) {
	if(args.length < 4 || !args[0].startsWith("rmi://")){
	    System.err.println("usage: java CalculatorClient rmi://host.domain:port/calculator <fp_number> <fp_number> <operation>\n" +
		    "i.e. postfix notation, where <operation> is to be substituted either by ADD,SUB, MUL or DIV" + 
	    "you may omit the port parameter, in which case it defaults to 1099");
	    return;
	}
	try{
	    
	    /*
	    //added this security manager code, win as server in vmware would bitch without it 
	    if (System.getSecurityManager() == null) {
		System.setProperty("java.security.policy", "calculator.policy");
		System.setSecurityManager(new RMISecurityManager());
	    }
	    */

	    Object lookupObj = Naming.lookup(args[0]);
	    Calculator calc = (Calculator) lookupObj;

	    //parseDouble() can throw a runtime exception
	    double firstOperand = Double.parseDouble(args[1]);
	    double secondOperand = Double.parseDouble(args[2]);

	    String operator = args[3];


	    if(operator.equalsIgnoreCase("ADD"))
		System.out.println("result: " + calc.add(firstOperand, secondOperand));
	    else if(operator.equalsIgnoreCase("SUB"))
		System.out.println("result: " + calc.subtract(firstOperand, secondOperand));
	    else if(operator.equalsIgnoreCase("MUL"))
		System.out.println("result: " +  calc.multiply(firstOperand, secondOperand));
	    else if(operator.equalsIgnoreCase("DIV"))
		System.out.println("result: " + calc.divide(firstOperand, secondOperand));
	    else throw new UnsupportedOperationException( args[3] + " unsupported parameter\n"+ 
	    "supported <operation> parameters: ADD SUB MUL DIV\n");

	}catch(MalformedURLException e){
	    System.err.println(args[0] + " is not a valid RMI URL, see exception " + e);
	}catch(RemoteException e){
	    System.err.println("Remote object threw exception " + e);
	    System.err.println("Cause is " + e.getCause());
	}catch(NotBoundException e){
	    System.err.println("Could not find the requested object on the server: " + e);
	}
    }

}
